import unittest
import subprocess
import tempfile
import os
import shutil

class TestEndToEnd(unittest.TestCase):
    """端到端系统测试"""
    
    def setUp(self):
        self.test_dir = tempfile.mkdtemp()
        self.splitter_cmd = ['python3', '-m', 'tools.main']
    
    def tearDown(self):
        shutil.rmtree(self.test_dir, ignore_errors=True)
    
    def test_complete_workflow_cut_command(self):
        """测试完整的cut命令工作流程"""
        output_dir = os.path.join(self.test_dir, 'output')
        
        # 执行cut命令
        cmd = self.splitter_cmd + [
            'cut',
            '-r', '24.03-LTS',
            '-a', 'x86_64',
            '-o', output_dir,
            'bash_bin'
        ]
        
        result = subprocess.run(cmd, capture_output=True, text=True)
        
        # 验证命令执行成功
        self.assertEqual(result.returncode, 0, f"Command failed: {result.stderr}")
        
        # 验证输出目录存在
        self.assertTrue(os.path.exists(output_dir))
        
        # 验证生成了预期的文件结构
        self.assertTrue(os.path.isdir(output_dir))
    
    def test_complete_workflow_gen_command(self):
        """测试完整的gen命令工作流程"""
        output_dir = os.path.join(self.test_dir, 'sdf_output')
        os.makedirs(output_dir, exist_ok=True)
        
        # 执行gen命令
        cmd = self.splitter_cmd + [
            'gen',
            '-r', '24.03-LTS',
            '-o', output_dir,
            '-p', 'brotli'
        ]
        
        result = subprocess.run(cmd, capture_output=True, text=True)
        
        # 验证命令执行
        if result.returncode != 0:
            print(f"Gen command stderr: {result.stderr}")
        
        # 验证输出了SDF文件或正确执行
        self.assertIn('output', result.stdout.lower() if result.stdout else '')
    
    def test_docker_wrapper_script(self):
        """测试Docker包装脚本"""
        docker_script = 'bin/splitter-docker.sh'
        
        if os.path.exists(docker_script):
            # 测试脚本是否存在且可执行
            self.assertTrue(os.access(docker_script, os.X_OK))
            
            # 测试帮助信息
            result = subprocess.run([docker_script, '--help'], 
                                  capture_output=True, text=True)
            self.assertEqual(result.returncode, 0)
            self.assertIn('Usage:', result.stdout)
    
    def test_installation_script(self):
        """测试安装脚本"""
        install_script = 'install.sh'
        
        if os.path.exists(install_script):
            self.assertTrue(os.access(install_script, os.X_OK))
    
    def test_command_integration(self):
        """测试命令集成"""
        # 测试主命令帮助
        result = subprocess.run(self.splitter_cmd + ['--help'], 
                              capture_output=True, text=True)
        self.assertEqual(result.returncode, 0)
        self.assertIn('cut', result.stdout)
        self.assertIn('gen', result.stdout)
    
    def test_error_handling_integration(self):
        """测试错误处理集成"""
        # 测试无效参数
        result = subprocess.run(self.splitter_cmd + [
            'cut',
            '-r', 'invalid-release',
            '-a', 'x86_64',
            '-o', '/tmp/test',
            'test_slice'
        ], capture_output=True, text=True)
        
        # 应该处理错误而不是崩溃
        self.assertNotEqual(result.returncode, 0)